package client

import (
	"context"
	"google.golang.org/grpc"
	"month6/common/proto/user"
)

type Handler func(ctx context.Context, client user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := user.NewUserClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func SuperLogin(ctx context.Context, req *user.SuperLoginReq) (*user.SuperLoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.SuperLogin(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SuperLoginResp), nil
}

func AddClass(ctx context.Context, req *user.AddClassReq) (*user.AddClassResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.AddClass(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AddClassResp), nil
}

func ListClass(ctx context.Context, req *user.ListClassReq) (*user.ListClassResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.ListClass(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.ListClassResp), nil
}

func DetailClass(ctx context.Context, req *user.DetailClassReq) (*user.DetailClassResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.DetailClass(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.DetailClassResp), nil
}

func UpdatedClassStatus(ctx context.Context, req *user.UpdatedClassStatusReq) (*user.UpdatedClassStatusResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.UpdatedClassStatus(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UpdatedClassStatusResp), nil
}

func AddRob(ctx context.Context, req *user.AddRobReq) (*user.AddRobResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.AddRob(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AddRobResp), nil
}

func UserBuyClass(ctx context.Context, req *user.UserBuyClassReq) (*user.UserBuyClassResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.UserBuyClass(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UserBuyClassResp), nil
}

func BrowseList(ctx context.Context, req *user.BrowseListReq) (*user.BrowseListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.BrowseList(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.BrowseListResp), nil
}

func BrowseList2(ctx context.Context, req *user.BrowseList2Req) (*user.BrowseList2Resp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.BrowseList2(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.BrowseList2Resp), nil
}

func AddOrder(ctx context.Context, req *user.AddOrderReq) (*user.AddOrderResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.AddOrder(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AddOrderResp), nil
}

func AliPay(ctx context.Context, req *user.AliPayReq) (*user.AliPayResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, client user.UserClient) (interface{}, error) {
		login, err := client.AliPay(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AliPayResp), nil
}
